package com.lcp.ui;

import com.jfoenix.controls.JFXTextArea;
import com.lcp.core.Context;
import com.lcp.core.ControlledStage;
import com.lcp.utils.R;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;


/**
 * @author lcp
 * @date 2020-08-23
 */
public class MainController extends StageController implements ControlledStage, Initializable {

    private StageController stageController;
    @FXML
    public HBox contentArea;
    @FXML
    public AnchorPane MainView;
    @FXML
    private HBox footerhbox;
    @FXML
    private HBox progressbox;

    @FXML
    public Button btnSave;
    @FXML
    public Button btnDownload;

    @FXML
    public Label runStatus;

    @FXML
    public Menuview1Controller menuview1Controller;


    @Override
    public void setStageController(StageController stageController) {
        this.stageController = stageController;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        contentArea.prefWidthProperty().bind(MainView.widthProperty());
        contentArea.prefHeightProperty().bind(MainView.heightProperty().subtract(20));
        refreshFooterSize();
    }


    public void refreshFooterSize() {
        footerhbox.prefWidthProperty().bind(MainView.widthProperty());
        progressbox.setAlignment(Pos.CENTER_RIGHT);
    }

    /**
     * @Date 2020年11月10日
     * @param path
     * @throws IOException
     */
    public void menubarClick(String path) throws IOException {
        skipView("/pages/layout/"+path);
    }


    /**
     * 改变下部AnchorPane的界面
     * @param pagePath
     * @throws IOException
     */
    private void skipView(String pagePath) throws IOException {
        System.out.println(pagePath);
        ObservableList<Node> scrolChildren = contentArea.getChildren();
        scrolChildren.clear();
        URL url = getClass().getClassLoader().getResource(pagePath);
        System.out.println(url);
        scrolChildren.add(FXMLLoader.load(url));
    }

    public static void initContentViewFrame(HBox jfxTablePane) {
        MainController mainCtrl = (MainController) Context.controllers.get(MainController.class.getSimpleName());
        jfxTablePane.prefWidthProperty().bind(mainCtrl.MainView.widthProperty());
        jfxTablePane.prefHeightProperty().bind(mainCtrl.MainView.heightProperty());
    }

    public void toIndex(){
        try {
            skipView(R.menuview1Res);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static MainController getMainController() {
        HashMap<String, Object> controllers = Context.controllers;
        return (MainController) controllers.get(MainController.class.getSimpleName());
    }
}
